<?php
// +----------------------------------------------------------------------
// | 十分彩网系统 [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Wechat\Controller\ApppayController;
/**
 * 首页
 */
class AppController extends ApppayController {
	public function wxpay(){
		$record = array ('status' => 0,'msg' => '抱歉，支付的订单不存在！' );
		$request=I('request.');
		$id = $request['id'];//订单ID
		$vars['content_type'] = $request['type'];
		$orderdata = M("order")->where(array("orderid"=>$id))->find();
		if(!empty($orderdata) && is_array($orderdata)){
			$vars['order_sn']=$orderdata['content_sn'];
		}
		$tmp=parent::payOrderCreate($vars);
		p($tmp);
		vendor('wxpay.WxPayPubHelper.WxPayPubHelper');
		$fail_url=U('user/center/index');
		if(!empty($packid)){
			$vars['money'] = $this->getpaymoney($packid);//支付套餐
		}
		$payController = new PayController();
		$order=$payController->payOrderCreate($vars);
		$totalPay=round($order["pay_money"]*100);
		$unifiedOrder = new \UnifiedOrder_pub();
		$unifiedOrder->setParameter("body","代理充卡");
		$unifiedOrder->setParameter("out_trade_no",$order["pay_sn"]);//商户订单号
		$unifiedOrder->setParameter("total_fee",$totalPay);
		$unifiedOrder->setParameter("notify_url",\WxPayConf_pub::NOTIFY_URL);
		$unifiedOrder->setParameter("trade_type","NATIVE");
		$unifiedOrder->setParameter("attach","weixin_qrcode");
		$unifiedOrderResult = $unifiedOrder->getResult();
		if ($unifiedOrderResult["return_code"] == "FAIL")
		{
			//商户自行增加处理流程
			echo "通信出错：".$unifiedOrderResult['return_msg']."<br>";
			exit;
		}
		elseif($unifiedOrderResult["result_code"] == "FAIL")
		{
			//商户自行增加处理流程
			echo "错误代码：".$unifiedOrderResult['err_code']."<br>";
			echo "错误代码描述：".$unifiedOrderResult['err_code_des']."<br>";
			exit;
		}
		elseif($unifiedOrderResult["code_url"] != NULL)
		{
			//从统一支付接口获取到code_url
			$code_url = $unifiedOrderResult["code_url"];
		}
		$this->assign("payfied", $unifiedOrderResult);
		$this->assign("fail_url", $fail_url);
		$this->assign("code_url", $code_url);
		$this->display();
	}
	public function getpaymoney($packid){//根据支付套餐ID获取支付金额
		$result = M('package')->field("packprice")
		->where(array("status"=>'1',"packid"=>$packid))
		->find();
		return $result['packprice'];
	}
	public function PaySuccessReturn($dingdan,$total,$paytype,$trade_no=''){
		parent::PaySuccessReturn($dingdan,$total,$paytype,$trade_no);
	}
	public function apicall(){
		if($_POST['packid']){
			$packid = I('post.packid');
		}
		if($_GET['packid']){
			$packid = I('get.packid');
		}
		$ptype = I('get.paytype');
		$ordersn = I('get.ordersn');
		$type = I('get.content_type');
		if(isset($ordersn)){
			$vars['order_sn'] = $ordersn;
		}
		vendor('wxpay.WxPayPubHelper.WxPayPubHelper');
		$fail_url=U('user/center/index');
		if(!empty($packid)){
			$vars['money'] = $this->getpaymoney($packid);//支付套餐
		}
		$payController = new PayController();
		$order=$payController->payOrderCreate($vars);
		$totalPay=round($order["pay_money"]*100);
		$jsApi = new \JsApi_pub();
		//通过code获得openid
		if (!isset($_GET['code']))
		{
			//触发微信返回code码
			$_url="?ordersn={$order['pay_sn']}&paytype={$ptype}&content_type={$type}&packid=$packid";
			$url = $jsApi->createOauthUrlForCode(urlencode(\WEIXINPAY_JS_API_CALL_URL.$_url));
			header("Location: $url");
		}else{
			//获取code码，以获取openid
			$code = $_GET['code'];
			$jsApi->setCode($code);
			$openid = $jsApi->getOpenId();
		
			//此处用于处理支付取消后，用户点击返回会返回到此界面（带过期的code），此时openid是空的
			if($openid==""){
				header("Location: {$fail_url}");
				exit;
			}
		}
		$unifiedOrder = new \UnifiedOrder_pub();
		$unifiedOrder->setParameter("openid","$openid");//商品描述
		$unifiedOrder->setParameter("body","代理充卡");//商品描述
		$timeStamp = time();
		$out_trade_no = WEIXINPAY_APPID."$timeStamp";
		$unifiedOrder->setParameter("out_trade_no",$order["pay_sn"]);//商户订单号
		$unifiedOrder->setParameter("total_fee",$totalPay);
		$unifiedOrder->setParameter("notify_url",\WxPayConf_pub::NOTIFY_URL);
		$unifiedOrder->setParameter("trade_type","JSAPI");
		$unifiedOrder->setParameter("attach","weixin_js");
		$prepay_id = $unifiedOrder->getPrepayId();
		$jsApi->setPrepayId($prepay_id);
		$jsApiParameters = $jsApi->getParameters();
		$this->assign('jsApiParameters',$jsApiParameters);
		$this->assign('totalPay',$totalPay);
		$this->assign('pay_sn',$order['pay_sn']);
		$this->assign("fail_url", $fail_url);
		$this->display();
	}
}
?>